<?php
	namespace Common\Logic;
	use Common\Model\GoodsModel;
	class GoodsLogic extends GoodsModel{
      var $errorMsg = '';
		/*
		* Function:		getGoodsList
		* Description:	商品列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getGoodsList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getGoodsFilter($filter);
			return $this->where($conditon)->order('sort desc,id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getGoodsFilter
		* Description:	商品列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getGoodsFilter($filter = ''){
			$conditon = array();
			if(empty($filter['is_show'])){
			    $conditon['status'] = 1;
			}
			if(!empty($filter['type'])){
			    $conditon['type'] = $filter['type'];
			}
			$conditon['is_deleted'] = 0;
			if(!empty($filter['goods_title'])){
				$conditon['goods_title'] = array('like','%'.$filter['goods_title'].'%');
			}
			if(!empty($filter['bn'])){
				$conditon['bn'] = array('like','%'.$filter['bn'].'%');
			}
			if(!empty($filter['cate_id'])){
				$conditon['cate_id'] = $filter['cate_id'];
			}
            if($filter['is_recommend']!=''){
				$conditon['is_recommend'] = $filter['is_recommend'];
			}
			return $conditon;
		}
		
		/*
		* Function:get:	getGoodsCount
		* Description:	商品列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getGoodsCount($filter = ''){
			$conditon = $this->getGoodsFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		addGoods
		* Description:	增加商品
		* @param:		$data array 增加的数组
		*/
		public function addGoods($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getGoodsInfo
		* Description:	获取商品信息 by row
		* @param:		$id int ID
		*/
		public function getGoodsInfo($id){
			return $this->where(array('id'=>$id))->find();
		}
		
		/*
		* Function:		updateGoods
		* Description:	更新商品
		* @param:		$data array 更新的数组
		*/
		public function updateGoods($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteGoods
		* Description:	删除商品
		* @param:		$id int ID
		*/
		public function deleteGoods($id){
			return $this->where(array('id'=>$id))->delete();
		}
		
		/*
		* Function:		getGoodsAllList
		* Description:	部门列表
		*/
		public function getGoodsAllList(){
			return $this->where(array('status'=>1))->select();
		}

      /*
		* Function: importProduct
		* Description:商品导入数据插入
		* @param:$productData array phpexcel 导入数据解析的数据 $filter array post条件数组
		*/
		public function importProduct($productData = '',$filter = ''){
			$n = 0;
			if(is_array($productData)&&count($productData)>0){
				foreach($productData as $k=>$v){
					if(!empty($v['A'])){
                  $data = [];
						$data['op_id'] = session(C('USER_AUTH_KEY'));
                  $data['addtime'] = date('Y-m-d H:i:s',time());
						$data['bn'] = trim($v['A']);
                  if(!$this->where(array('bn'=>$data['bn']))->find()){
                     $data['stock_code'] = trim($v['B']);
                     $data['en_goods_title'] = trim($v['C']);
                     $data['goods_title'] = trim($v['D']);
                     $data['cate_id'] = trim($v['F']);
                     $data['age'] = trim($v['G']);
                     if($lastId = $this->addGoods($data)){
                        $itemData = [];
                        $itemData['goods_id'] = $lastId;
                        $itemData['goods_title'] = $data['goods_title'];
                        $itemData['goods_spec'] = '默认';
                        $itemData['market_price'] = trim($v['E']);
                        $itemData['selling_price'] = trim($v['E']);
                        $itemData['retail_price'] = trim($v['E']);
                        $itemData['addtime'] = $data['addtime'];
                        if(!D('GoodsList')->addGoodsList($itemData)){
                           $this->errorMsg = '商品明细添加失败';
                           return false;
                        }
                        $n++;
                     }else{
                        $this->errorMsg = '条形码：'.$data['bar_code'].'添加失败！';
                        return false;
                     }
                     // $this->errorMsg = '货号：'.$data['bn'].'已存在！';
							// return false;
                  }
					}
				}
			}
			return '成功导入'.$n.'条数据';
		}

      /*
		* 自动生成商品货号
		*/
		public function product_bn_create($type=''){
			do{
				$j++;
				$bn = (date('Ymd').'0000')+$j;
				$row = $this->where(array('bn'=>$bn))->find();
			}while($row);
			return $type.$bn;
		}

      /*
		* Function: get_error
		* Description:获取错误信息errorMsg
		*/
		public function get_error(){
			return $this->errorMsg;
		}

	}
?>